import React from "react";
import { AsyncStorage } from "react-native";

import Config from "../config";
import Constants from "../common/Constants";
import ResponseConstants from "../common/ResponseConstants";
import BaseModelNew from "./BaseModelNew";

//用户数据
export default class UserModel extends BaseModelNew {
  constructor() {
    super();

    this._selfData = UserData;
    this.getSelfData = this.getSelfData.bind(this);
    this.checkIsSelf = this.checkIsSelf.bind(this);
    this.setUserData = this.setUserData.bind(this);
    this.clearUserData = this.clearUserData.bind(this);
    this.sendRequestDianzanPosts = this.sendRequestDianzanPosts.bind(this);
    this.sendRequestShoucangPosts = this.sendRequestShoucangPosts.bind(this);
    this.sendRequestFabuPosts = this.sendRequestFabuPosts.bind(this);
    this.sendRequestSelfData = this.sendRequestSelfData.bind(this);
    this.sendUpdatePassword = this.sendUpdatePassword.bind(this);
    this.sendUpdateSelfData = this.sendUpdateSelfData.bind(this);
    this.updateUserData = this.updateUserData.bind(this);
    this.requestAppConfig = this.requestAppConfig.bind(this);
    this.requestAppConfigFromServer = this.requestAppConfigFromServer.bind(
      this
    );
    this.sendUpdatePhone = this.sendUpdatePhone.bind(this);
    this.requestOtherUserData = this.requestOtherUserData.bind(this);
    this.sendForgetPwd = this.sendForgetPwd.bind(this);
    this.requestNoticeList = this.requestNoticeList.bind(this);
    this.requestSaerchList = this.requestSaerchList.bind(this);
    this.requestMsgSystem = this.requestMsgSystem.bind(this);
    this.requestMsgComments = this.requestMsgComments.bind(this);
  }

  // DqappUserDetail {
  //   avatar_url (string, optional): 头像,
  //   department (string, optional): 所属单位,
  //   ee_no (integer, optional): 员工编号,
  //   mobile (string, optional): 手机号码,
  //   name (string, optional): 员工名字,
  //   nick (string, optional): 昵称,
  //   perms (Array[string], optional): 权限集
  // }
  setup(dataManager, callback) {
    this.dataManager = dataManager;
    this.getSaveData(callback);
  }

  //初始化用户
  setUserData(initUserData) {
    this._selfData = initUserData;
    Constants.USER = initUserData;

    this.saveSelfData();
  }

  updateUserData(userData) {
    if (userData.ee_no) {
      this._selfData.ee_no = userData.ee_no;
    }
    if (userData.name) {
      this._selfData.name = userData.name;
    }
    this.saveSelfData();
  }

  clearUserData() {
    this._selfData = {};
    Constants.USER = {};
    this.saveSelfData();
  }
  //获取系统消息列表
  requestMsgSystem(data, callback) { 
    var url = Config.serverUrl + "/msgs/system?page="
      +data.page+"&page_size=10";
    if(data.type!==undefined) { // 类型, 1系统消息，2运动消息, 3运动消息
      url += "&type=" + data.type;
    }
    this.sendGet(url, callback);
  }
  //获取评论消息列表  GET /dqapp/msgs/comments
  requestMsgComments(data, callback) { 
    var url = Config.serverUrl + "/msgs/comments?page="
      +data.page+"&page_size=10";
    if(data.type!==undefined) { // 类型, 0全部,1新闻，2金点子, 3圈子
      url += "&type=" + data.type;
    }
    this.sendGet(url, callback);
  }
  getSaveData(callback) {
    AsyncStorage.getItem(
      "selfData",
      function(errs, result) {
        //TODO:错误处理
        if (!errs) {
          if (result == null) {
            this._selfData = {};
          } else {
            let selfData = JSON.parse(result);
            this._selfData = selfData;
          }
          console.log("Get selfData success", result);
        } else {
          this._selfData = {};
          console.log("Get selfData error");
        }
        Constants.USER = this._selfData;
        callback();
      }.bind(this)
    );
  }

  saveSelfData() {
    let selfData = JSON.stringify(this._selfData);
    AsyncStorage.setItem("selfData", selfData, function(errs) {
      //TODO:错误处理
      if (errs) {
        console.log("存储selfData错误");
      }
      if (!errs) {
        console.log("存储selfData无误");
      }
    });

    this.dataManager.getModel(Constants.MODULE_TYPE.PUSH).initAlias();
  }

  //获得用户数据
  getSelfData() {
    return this._selfData;
  }

  checkIsSelf(userId) {
    return userId == this._selfData.user_id;
  }

  //http://118.89.111.124:8080/dqapp/boards/1/posts/liked?page=1&page_size=5
  //我的点赞
  sendRequestDianzanPosts(actionData, callback) {
    var url =
      Config.serverUrl +
      "/boards/1/posts/liked?page=" +
      actionData.page +
      "&page_size=" +
      10;
      if (actionData.keyword==='news') {
         url =Config.serverUrl +"/news/liked?page=" +actionData.page +"&page_size=" +10;
      }
      if (actionData.keyword==='posts') {
         url =Config.serverUrl +"/boards/0/posts/liked?page=" +actionData.page +"&page_size=" +10;
      }
    this.sendGet(url, callback);
  }

  //我的收藏
  //http://118.89.111.124:8080/dqapp/boards/1/posts/favorited?page=1&page_size=5
  sendRequestShoucangPosts(actionData, callback) {
    var url =
      Config.serverUrl +
      "/boards/1/posts/favorited?page=" +
      actionData.page +
      "&page_size=" +
      10;

    if (actionData.keyword==='news') {
         url =Config.serverUrl +"/news/favorited?page=" +actionData.page +"&page_size=" +10;
    }
      if (actionData.keyword==='posts') {
         url =Config.serverUrl +"/boards/1/posts/favorited?page=" +actionData.page +"&page_size=" +10;
    }
    this.sendGet(url, callback);
  }

  //我的发布
  //http://118.89.111.124:8080/dqapp/boards/1/posts/owned?page=1&page_size=5
  sendRequestFabuPosts(page, callback) {
    var url =
      Config.serverUrl +
      "/boards/1/posts/owned?page=" +
      page +
      "&page_size=" +
      10;
    this.sendGet(url, callback);
  }

  //请求个人数据
  ////http://118.89.111.124:8080/dqapp/myself
  sendRequestSelfData(callback) {
    var url = Config.serverUrl + "/myself";
    this.sendGet(url, callback);
  }

  //修改密码
  //http://47.93.32.102:8081/dqauth/app/passwd
  // {
  //   "new_passwd": "21dmoxgzq0",
  //   "sms_code": "g7o4rz"
  // }
  sendUpdatePassword(data, callback) {
    var sendData = {
      new_passwd: data.password + "",
      sms_code: data.smsNum + ""
    };
    this.sendPost(
      Config.serverUrlHttps + "/app/passwd",
      JSON.stringify(sendData),
      callback
    );
  }
  //修改登陆手机号
  //http://47.93.32.102:8081/dqauth/app/login_mobile
  // {
  //   "passwd": "21dmoxgzq0",
  //   "sms_code": "g7o4rz"
  // }
  sendUpdatePhone(data, callback) {
    var sendData = {
      mobile:data.mobile+"",
      new_mobile:data.new_mobile+"",
      passwd: data.password + "",
      sms_code: data.smsNum + ""
    };
    this.sendPost(
      Config.serverUrlHttps + "/app/login_mobile",
      JSON.stringify(sendData),
      callback
    );
  }

  sendForgetPwd(data, callback) {
    var sendData = {
      id: data.id + "",
      new_passwd: data.password + "",
      sms_code: data.smsNum + ""
    };
    this.sendPost(
      Config.serverUrlHttps + "/app/login_passwd",
      JSON.stringify(sendData),
      callback
    );
  }

  //修改个人资料
  ////http://118.89.111.124:8080/dqapp/myself
  sendUpdateSelfData(data, callback) {
    var url = Config.serverUrl + "/myself";
    this.sendPatchNew(url, data, callback);
  }

  //从后台请求配置，老的做法
  requestAppConfig(data, callback) {
    if (data.requestType === "web") {
      this.sendRequestFile(Config.appConfigUrl, callback);
    } else {
      this.requestAppConfigFromServer(data, callback);
    }
  }

  //从服务器请求配置
  //http://dqtest.bestplus.me/dqapp/config?key=222
  requestAppConfigFromServer(data, callback) {
    this.sendGet(
      Config.serverUrl + "/config?key=" + data.key,
      callback,
      ResponseConstants.responseType_json,
      true
    );
  }

  //请求某些用户的数据
  //http://dqtest.bestplus.me/dqapp/users/selected?id=3423472387492374234
  requestOtherUserData(data, callback) {
    let forceRequest = false;
    if (data.forceRequest === true) {
      forceRequest = true;
    }
    this.sendGet(
      Config.serverUrl + "/users/selected?id=" + data.id,
      callback,
      ResponseConstants.responseType_json,
      forceRequest
    );
  }

  requestNoticeList(data, callback) {
    var url =
      Config.serverUrl +
      "/news?page=" +
      data.page +
      "&page_size=" +
      10 +
      "&sort_id=1&tag=" +
      1 +
      "&home_page=" +
      0;
    this.sendGet(url, callback);
  }
  requestSaerchList(data, callback) {
    var url =
      Config.serverUrl +
      "/search/dqapp/search/all?key=" +
      data.key +
      "&sort=1&page=" +
      data.page +
      "&page_size=" +
      data.page_size||10;
    this.sendGet(url, callback);
  }
}

var UserData = {
  userId: -1 //用户id
};
